<?php

namespace app\admin\model\wdsxh;

use think\Model;
use traits\model\SoftDelete;
use think\Db;
use fast\Tree;

class DiyPage extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wdsxh_diy_page';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['home' => __('Status home'), 'custom' => __('Status custom')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 页面默认样式数据
     */
    public function getDefaultStyle($url)
    {
        return [
            // 在线视频
            'videoDiy' => [
                'name' => '在线视频',
                'type' => 'videoDiy',
                'params' => [
                    'videoUrl' => 'https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/%E7%AC%AC1%E8%AE%B2%EF%BC%88uni-app%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D%EF%BC%89-%20DCloud%E5%AE%98%E6%96%B9%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B@20200317.mp4',
                    'poster' => '/assets/addons/wdsxh/img/video_bg.png',
                    'autoplay' => '2'
                ],
                'style' => [
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'background' => '#ffffff',
                ]
            ],
            // 图片轮播
            'carouselDiy' => [
                'name' => '图片轮播',
                'type' => 'carouselDiy',
                'style' => [
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'borderRadius' => 0,
                    'background' => '#ffffff',
                ],
                'params' => [
                    'type' => 'normal',
                    'interval' => '3000',
                ],
                'data' => [
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/banner.jpg',
                        'link' => null
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/banner.jpg',
                        'link' => null
                    ]
                ]
            ],
            // 单图组
            'imagesDiy' => [
                'name' => '单图组',
                'type' => 'imagesDiy',
                'style' => [
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'borderRadius' => 0,
                    'background' => '#ffffff'
                ],
                'data' => [
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/banner.jpg',
                        'link' => null
                    ]
                ]
            ],
            // 导航组
            'navDiy' => [
                'name' => '导航组',
                'type' => 'navDiy',
                'style' => ['background' => '#ffffff', 'borderRadius' => 0,'rowsNum' => '4'],
                'data' => [
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字1',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字2',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字3',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字4',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字5',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字6',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字7',
                        'color' => '#666666'
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                        'link' => null,
                        'text' => '按钮文字8',
                        'color' => '#666666'
                    ]
                ]
            ],
            // 图片魔方
            'cubeDiy' => [
                'name' => '图片魔方',
                'type' => 'cubeDiy',
                'style' => [
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'background' => '#ffffff',
                    'borderRadius' => 0,
                    'layout' => '2'
                ],
                'data' => [
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/01.jpg',
                        'link' => null
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/02.jpg',
                        'link' => null
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/03.jpg',
                        'link' => null
                    ],
                    [
                        'imgUrl' => '/assets/addons/wdsxh/img/04.jpg',
                        'link' => null
                    ]
                ],
                'dataNum' => 4
            ],
            // 信息卡片
            'infoCardDiy' => [
                'name' => '信息卡片',
                'type' => 'infoCardDiy',
                'params' => [
                    'title' => '此处是信息卡片标题',
                    'image' => '/assets/addons/wdsxh/img/menus.png',
                    'content' => '此处是信息卡片内容',
                    'btnTxt' => '查看详情',
                    'link' => null
                ],
                'style' => [
                    'borderRadius' => 0,
                    'btnBorderRadius' => 0,
                    'background' => '#FFFFFF',
                    'btnBackground' => '#409EFF',
                    'btnColor' => '#FFFFFF',
                    'paddingTop' => 10,
                    'paddingLeft' => 10,
                ]
            ],
            // 文字按钮
            'textButtonDiy' => [
                'name' => '文字按钮',
                'type' => 'textButtonDiy',
                'style' => [
                    'background' => '#ffffff',
                    'paddingTop' => 5,
                    'paddingLeft' => 0,
                    'fontSize' => 14,
                ],
                'data' => [
                    [
                        'link' => null,
                        'text' => '按钮文字1',
                        'color' => '#666666'
                    ],
                    [
                        'link' => null,
                        'text' => '按钮文字2',
                        'color' => '#666666'
                    ],
                    [
                        'link' => null,
                        'text' => '按钮文字3',
                        'color' => '#666666'
                    ]
                ]
            ],
            // 文章
            'articleDiy' => [
                'name' => '文章',
                'type' => 'articleDiy',
                'params' => [
                    'showImg' => 'show',
                    'link' => null
                ],
                'style' => [
                    'borderRadius' => 0,
                    'background' => '#FFFFFF',
                    'paddingTop' => 10,
                    'paddingLeft' => 10,
                ]
            ],
            // 广告组件
            'adDiy' => [
                'name' => '广告组件',
                'type' => 'adDiy',
                'style' => [
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'background' => '#ffffff',
                ],
                'params'=>[
                    'type' => 'banner',
                    'adid' => ''
                ]
            ],
            // 标题单元
            'titleUnitDiy' => [
                'name' => '标题单元',
                'type' => 'titleUnitDiy',
                'style' => [
                    'paddingTop' => 8,
                    'paddingLeft' => 12,
                    'background' => '#ffffff',
                    'textColor' => '#0441f5',
                    'rightColor' => '#999999',
                ],
                'params'=>[
                    'title1' => '明文标题',
                    'title2' => 'CIPHERTEXT',
                    'title3' => '更多',
                    'link' => null
                ]
            ],
            // 图文按钮
            'buttonDiy' => [
                'name' => '图文按钮',
                'type' => 'buttonDiy',
                'params' => [
                    'imgUrl' => '/assets/addons/wdsxh/img/menus.png',
                    'showImg' => true,
                    'text' => '默认按钮',
                    'link' => null,
                ],
                'style' => [
                    'background' => '#ffffff',
                    'paddingTop' => 10,
                    'paddingLeft' => 10,
                    'btnBackground' => '#409EFF',
                    'borderColor' => '',
                    'color' => '#ffffff',
                    'height' => 36,
                    'borderSize' => 1,
                    'fontSize' => 14,
                    'iconSize' => 20,
                    'borderRadius' => 5,
                ]
            ],
            // 地图
            'mapDiy' => [
                'name' => '地图',
                'type' => 'mapDiy',
                'params' => [
                    'latitude' => 36.04,
                    'longitude' => 120.2,
                ],
                'style' => [
                    'height' => 60,
                    'paddingTop' => 0,
                    'paddingLeft' => 0,
                    'background' => '#ffffff'
                ]
            ],
            // 标题
            'titleDiy' => [
                'name' => '标题',
                'type' => 'titleDiy',
                'style' => [
                    'paddingTop' => 10,
                    'paddingLeft' => 16,
                    'background' => '#ffffff',
                    'fontSize' => 14,
                    'color' => "#000000",
                    'btnSize' => 12,
                    'btnColor' => "#888888",
                    'fontStyle' => "normal",
                ],
                'params' => [
                    'title' => "标题文字",
                    'btnText' => "查看更多",
                    'link' => null,
                ]
            ],
            // 辅助线条
            'lineDiy' => [
                'name' => '辅助线条',
                'type' => 'lineDiy',
                'style' => [
                    'background' => '#ffffff',
                    'lineStyle' => 'solid',
                    'lineHeight' => '1',
                    'lineColor' => "#000000",
                    'paddingTop' => 10,
                    'paddingLeft' => 0,
                ]
            ],
            // 广播
            'noticeDiy' => [
                'name' => '广播',
                'type' => 'noticeDiy',
                'params' => [
                    'text' => '这里是第一条来自后台自定义广播的信息',
                    'icon' => '/assets/addons/wdsxh/img/notice.png',
                    'showImg' => true,
                ],
                'style' => [
                    'paddingTop' => 5,
                    'paddingLeft' => 10,
                    'background' => '#ffffff',
                    'textColor' => '#000000',
                    'fontSize' => 14,
                    'iconSize' => 16,
                ]
            ],
            // 辅助空白
            'blankDiy' => [
                'name' => '辅助空白',
                'type' => 'blankDiy',
                'style' => [
                    'height' => '20',
                    'background' => '#d9d9d9'
                ]
            ],
            // 文本组
            'textDiy' => [
                'name' => '文本组',
                'type' => 'textDiy',
                'style' => [
                    'paddingTop' => 5,
                    'paddingLeft' => 10,
                    'background' => '#ffffff',
                    'text' => '这里是文本的内容',
                    'textColor' => '#000000',
                    'fontsize' => 14,
                    'fontStyle' => 'normal',
                    'textAlign' => 'left',
                ]
            ],
            // 背景音乐
            'bgmDiy' => [
                'name' => '背景音乐',
                'type' => 'bgmDiy',
                'params' => [
                    'playImgUrl' => '/assets/addons/wdsxh/img/play.png',
                    'stopImgUrl' => '/assets/addons/wdsxh/img/stop.png',
                    'musicUrl' => ''
                ],
                'style' => [
                    'right' => 2,
                    'bottom' => 30,
                    'opacity' => 100
                ]
            ],
            // 富文本
            'richTextDiy' => [
                'name' => '富文本',
                'type' => 'richTextDiy',
                'params' => [
                    'content' => '<span>这里是富文本的内容</span>',
                ],
                'style' => [
                    'paddingTop' => 12,
                    'paddingLeft' => 12,
                    'borderRadius' => 0,
                    'background' => '#ffffff'
                ]
            ],
            // 警告提示
            'warnDiy' => [
                'name' => '警告提示',
                'type' => 'warnDiy',
                'params' => [
                    'type' => 'success',
                    'title' => '提示标题',
                    'description' => '提示内容1，提示内容2，提示内容3，提示内容4，提示内容5',
                    'closable' => true,
                    'showIcon' => true,
                    'effect' => "light",
                ],
                'style' => [
                    'paddingTop' => 5,
                    'paddingLeft' => 5,
                    'background' => '#ffffff',
                ],
            ],
            // 时间线
            'timelineDiy' => [
                'name' => '时间线',
                'type' => 'timelineDiy',
                'style' => [
                    'paddingTop' => 10,
                    'paddingLeft' => 10,
                    'background' => '#ffffff',
                ],
                'data' => [
                    [
                        'time' => date('Y-m-d'),
                        'color' => '#0FAFFF',
                        'hide' => true,
                        'content' => '时间节点1',
                    ],
                    [
                        'time' => date('Y-m-d'),
                        'color' => '#0FAFFF',
                        'hide' => true,
                        'content' => '时间节点2',
                    ],
                    [
                        'time' => date('Y-m-d'),
                        'color' => '#0FAFFF',
                        'hide' => true,
                        'content' => '时间节点3',
                    ],
                ],
            ],
            // 悬浮按钮
            'floatDiy' => [
                'name' => '悬浮按钮',
                'type' => 'floatDiy',
                'params' => [
                    'type' => 'service',
                    'image' => '/assets/addons/wdsxh/img/service.png',
                    'link' => null
                ],
                'style' => [
                    'right' => 2,
                    'bottom' => 10,
                    'opacity' => 100
                ]
            ],
        ];
    }

    /**
     * 页面默认数据
     */
    public function getDefaultPageData()
    {
        static $defaultPage = [];
        if (!empty($defaultPage)) return $defaultPage;
        return [
            'type' => -1,
            'name' => '页面设置',
            'params' => [
                'name' => '模板名称',
                'title' => '首页',
                'share_title' => '分享标题',
            ],
            'style' => [
                'titleTextColor' => 'black',
                'titleBackgroundColor' => '#fff',
            ]
        ];
    }

    /**
     * 获取内置链接列表
     */
    public function getLinkUrl()
    {            
        return [
            'Inlay'=> ['type' => 'Inlay','name' => '内置页面'],
            'Custom'=> ['type' => 'Custom','name' => '固定页','list' => $this->getCustomLink()],
            'Member'=> ['type' => 'Member','name' => '会员列表','list' => $this->getMemberLink()],
            'Company'=> ['type' => 'Company','name' => '公司列表','list' => $this->getCompanyLink()],
            'Activity'=> ['type' => 'Activity','name' => '活动列表','list' => $this->getActivityLink()],
            'Article'=> ['type' => 'Article','name' => '文章列表','list' => $this->getArticleLink()],
            'WXMp'=> ['type' => 'WXMp','name' => '小程序'],
            'Outside'=> ['type' => 'Outside','name' => '外链'],
            'Phone'=> ['type' => 'Phone','name' => '拨打电话'],
        ];
    }

    

    /**
     * 获取Url列表
     */
    public function getUrlList()
    {
        return $this->where('status','custom')->order(['id' => 'desc'])->select();
    }

    /**
     * 获取链接列表
     */
    public function getCustomLink()
    {
        $data = Db::name('wdsxh_link')
            ->where('status','1')
            ->field('name title,url path')
            ->order('weigh desc')
            ->select();
        return $data;
    }

    /**
     * 获取用户列表
     */
    public function getMemberLink()
    {
        $current_date = date('Y-m-d',time());
        $data = Db::name('wdsxh_member')
            ->where('status','1')
            ->where('expiretime', '>=', $current_date)
            ->field('id,name title,mobile path')
            ->order('id desc')
            ->select();
        foreach ($data as $k=>$v) {
            $data[$k]['path'] = '/pages/Component/Module/Main/Program/member/index/index?id='.$v['id'];
        }
        return $data;
    }

    //获取公司列表
    public function getCompanyLink()
    {
        $data = Db::name('wdsxh_member_unit')
            ->where('status','1')
            ->field('id,company_name title,member_id path')
            ->order('weigh desc,id desc')
            ->select();
        foreach ($data as $k=>$v) {
            $data[$k]['path'] = '/pages/Component/Module/Main/Program/company/index/index?id='.$v['id'];
        }
        return $data;
    }

    //获取活动列表
    public function getActivityLink()
    {
        $data = Db::name('wdsxh_activity')
            ->field('id,title,type path')
            ->order('weigh desc,id desc')
            ->select();
        foreach ($data as $k=>$v) {
            $data[$k]['path'] = '/pages/Component/Module/Main/Program/activity/detail/index?id='.$v['id'];
        }
        return $data;
    }

    //获取文章列表
    public function getArticleLink()
    {
        $tree = Tree::instance();
        $this->model = new ArticleCat();
        $tree->init(collection($this->model->order('weigh desc,id desc')->field('id,name,weigh path,pid')->select())->toArray(), 'pid');
        $data = $tree->getTreeList($tree->getTreeArray(0), 'name');
        foreach ($data as $k=>&$v) {
            $data[$k]['title'] = str_replace('&nbsp;','',$v['name']);
            $data[$k]['path'] = '/pages/Component/Module/Article/Program/list/index?id='.$v['id'];
        }
        return $data;
    }
}
